package com.winz.cultureapp.activity;

import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.vp.NewsVpAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.entity.VolunteerTypeBean;
import com.winz.cultureapp.fragment.ShowVolunteerFragment;
import com.winz.cultureapp.fragment.VolunteerNewsFragment;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 志愿活动
 */
public class VolunteerActivity extends BaseAppCompatActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.stb_volunteer)
    SlidingTabLayout mStb_volunteer;
    @BindView(R.id.vp_volunteer)
    ViewPager mVp_volunteer;

    private String[] titles;
    private List<Fragment> fragments = new ArrayList<>();
    private NewsVpAdapter mAdapter;

    @Override
    protected int contentView() {
        return R.layout.activity_volunteer;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText(R.string.activity_top_news);
//        fragments.add(NewsFragment.getInstance(titles[0]));
//        fragments.add(ShowActivityFragment.getInstance());
//        fragments.add(ShowActivityFragment.getInstance());
//        fragments.add(VolunteerActivityFragment.getInstance());

    }

    @Override
    protected void initEvents() {

    }
    @Override
    protected void loadDatas(Intent intent) {
        // 获取全部志愿活动分类列表
        getVolunteerType();
    }

    /**
     * 获取全部志愿活动分类列表
     */
    private void getVolunteerType() {
        showProgressDialog();
        HttpUitls.onGet("volunteer_category",new OnResponeListener<VolunteerTypeBean>() {
            @Override
            public void onSuccess(VolunteerTypeBean bean) {
                Logger.d("volunteer_category==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    // 可以初始化导航
                    initTitles(bean.data);
                    // 初始化界面
                    initFragments(bean.data);

                    mAdapter = new NewsVpAdapter(getSupportFragmentManager(), fragments, titles);
                    mVp_volunteer.setAdapter(mAdapter);

                    mStb_volunteer.setViewPager(mVp_volunteer, titles);
                    mVp_volunteer.addOnPageChangeListener(VolunteerActivity.this);
                    mVp_volunteer.setOffscreenPageLimit(titles.length);
                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                Logger.d("volunteer_category==>>", error);
                dismissProgressDialog();
            }
        });
    }

    /**
     * 初始化导航标题
     * @param data
     */
    private void initTitles(VolunteerTypeBean.DataBean data) {
        if (data.list.size()>0){

            titles = new String[data.list.size() + 1];
            titles[0] = "新闻资讯";
//            titles[1] = "文化志愿活动";
            for (int i=0;i<data.list.size();i++){
                titles[i + 1] = data.list.get(i).categoryName;
            }
        }
    }

    /**
     * 初始化界面
     */
    private void initFragments(VolunteerTypeBean.DataBean data) {
        // 新闻资讯
        fragments.add(VolunteerNewsFragment.getInstance());
        // 文化志愿活动
//        fragments.add(ShowActivityFragment.getInstance());

        // 添加动态部分
        if (data.list.size()>0){
            for (int i=0;i<data.list.size();i++){
                fragments.add(ShowVolunteerFragment.getInstance(data.list.get(i).volunteerCategoryId));
            }
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTv_top_title.setText(titles[position]);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @OnClick({R.id.fl_back})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.fl_back:
                finish();
                break;
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
